export class EditorAttrItem {
    name = "";
    property = "";
    ctor: any = null;
    type = "";
};

export class EditorAttrConfig {
    items: EditorAttrItem[] = [];
    initExtends = false;
    initTypes = false;
    ctor: any = null;
};

export const EditorAttrMap = new Map<(new () => any), EditorAttrConfig>();
//注册属性
export function EditorAttr(attrName: string = "", type = "any") {
    return (target: Object, property: string): void => {
        let ctor: any = target.constructor;
        let eac = EditorAttrMap.get(ctor);
        if (!eac) {
            EditorAttrMap.set(ctor, new EditorAttrConfig());
            eac = EditorAttrMap.get(ctor);
            eac.ctor = ctor;
        }

        if (!attrName) {
            attrName = property;
        }
        let item = new EditorAttrItem();
        item.ctor = ctor;
        item.name = attrName;
        item.property = property;
        item.type = type;

        // console.log("注册属性：", attrName, ctor.name, property);
        eac.items.push(item);
    };
};

export const GetEditorAttrListByTarget = function (target: any): EditorAttrConfig {
    let ctor = target.constructor;
    let parent = Object.getPrototypeOf(ctor);
    //@ts-ignore
    let eac = EditorAttrMap.get(ctor);
    if (!eac) {
        EditorAttrMap.set(ctor, new EditorAttrConfig());
        eac = EditorAttrMap.get(ctor);
        eac.ctor = ctor;
    }
    if (!eac.initExtends) {
        while (parent && parent.name !== "cc.Component") {
            let pEac = EditorAttrMap.get(parent);
            parent = Object.getPrototypeOf(parent);
            if (pEac) {
                eac.items = pEac.items.concat(eac.items);
            }
        }
        eac.initExtends = true;
    }

    if (!eac.initTypes) {
        //根据target值，初始化类型
        eac.items.forEach(item => {
            if (item.type === "any") {
                let val = target[item.property];
                switch (true) {
                    case val instanceof cc.Color:
                        item.type = "color";
                        break;
                    case val instanceof cc.Vec2:
                        item.type = "vec2";
                        break;
                    case val instanceof cc.Vec3:
                        item.type = "vec3";
                        break;
                    case val instanceof cc.Vec4:
                        item.type = "vec4";
                        break;
                    case typeof val === "boolean":
                        item.type = "boolean";
                        break;
                    case typeof val == "number":
                        item.type = "number";
                        break;
                    case typeof val == "string":
                        item.type = "string";
                        break;
                    default:
                        item.type = "string";
                        break;
                }
            }
        });
        eac.initTypes = true;
        console.log("eac初始化成员的数据类型：", eac);
    }

    return eac;
}

window["EditorAttrMap"] = EditorAttrMap;